
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using Swaf.BizObj;

namespace Swaf.BizObj.fieldValidator
{
	/// <summary> The concrete FieldValidator that contains a group
	/// of arbitrary Validators (the Composite Design Pattern).
	/// </summary>
	[Serializable]
	public class CompositeValidator:FieldValidator
	{
		
		public override string validateNoThrow(IBizObj bizObj, string name, string newValue)
		{
			string ans = null;
			foreach(FieldValidator validator in list_)
				ans = validator.validateNoThrow(bizObj, name, newValue);
			return ans;
		}
		
		/// <summary>This should never be called.
		/// </summary>
		protected internal override FieldValidator create(string context, string a, string b, string c)
		{
			parseError("Unexpected use of CompositeValidator.create(): " + context);
			return null;
		}
		
		/// <summary>
		/// Constructs a CompositeValidator using the specified Collection of Validators, 'validators'.</summary>
		/// <param name="s">the String that was parsed to produce 'validators'.</param>
		/// <param name="validators">a Collection of FieldValidator objects.
		/// No element of 'validators' may be 'null'.</param>
		public CompositeValidator(string s, ICollection validators)
			:base(s)
		{
			list_ = new ArrayList(validators);
		}
		
		/// <summary>These are the Validators in this Composite.</summary>
		protected internal IList list_;
		
		public override string details(string prefix)
		{
			System.Text.StringBuilder ans = new System.Text.StringBuilder();
			ans.Append(prefix).Append("CompositeValidator(\"").Append(this.ToString()).Append("\") {\n");
			foreach(FieldValidator validator in list_)
				ans.Append(validator.details("\t" + prefix)).Append("\n");
			ans.Append(prefix).Append("}");
			return ans.ToString();
		}
	}
}